//
//  CZEmoticonTools.swift
//  weibo
//
//  Created by 丁蒙 on 16/7/5.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit
//显示的列数
let EmoticonMaxCol = 7
//显示的行数
let EmoticonMaxRow = 3
//每页显示的个数
let EmoticonMaxCount = EmoticonMaxCol * EmoticonMaxRow - 1

class CZEmoticonTools: NSObject {
    //全局访问点
    static let sharedEmoticonTools: CZEmoticonTools = CZEmoticonTools()
    
    // 01 获得bundle
    lazy var emoticonBundle: NSBundle = {
        // 获取bundle路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        // 返回bundle
        return NSBundle(path: path)!
    }()

    
    //默认表情一维数组
    lazy var defaultEmoticonsArray: [CZEmoticons] = {
        return self.getEmoticons("default/")
    }()
    //emoji表情一维数组
    lazy var emojiArray: [CZEmoticons] = {
        return self.getEmoticons("emoji/")
    }()
    //lxh表情一维数组
    lazy var lxhArray: [CZEmoticons] = {
        return self.getEmoticons("lxh/")
    }()
    
    //三维数组
    lazy var allEmoticons: [[[CZEmoticons]]] = {
       return [
        self.getEmoticonsGroup(self.defaultEmoticonsArray),
        self.getEmoticonsGroup(self.emojiArray),
        self.getEmoticonsGroup(self.lxhArray)
        ]
    }()
    
    // MARK: - 获取bundle文件
    func emoticonBundile() -> NSBundle {
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        let bundle = NSBundle(path: path)!
        
        return bundle
    }
    
    // MARK: - 获取一维数组的方法
    func getEmoticons(fileName: String) -> [CZEmoticons] {
        let path = emoticonBundile().pathForResource("\(fileName)info.plist", ofType: nil)!
        
        let array = NSArray(contentsOfFile: path)!
        //临时数组
        var temArray:[CZEmoticons] = [CZEmoticons]()
        
        for dict in array {
            let emoticon = CZEmoticons(dict: dict as! [String: AnyObject])
            //获取路径
            emoticon.path = "\(fileName)"
            
            temArray.append(emoticon)
        }
        return temArray
    }
    
    // MARK: - 获取二维数组的方法
    func getEmoticonsGroup(emoticonsArr: [CZEmoticons]) -> [[CZEmoticons]] {
        //        0-19 20-39
        //总页数
        let pageCount = (emoticonsArr.count + EmoticonMaxCount - 1) / EmoticonMaxCount
        
        //临时数组
        var temArray:[[CZEmoticons]] = [[CZEmoticons]]()
        
        for i in 0..<pageCount {
            let loc = EmoticonMaxCount * i
            var len = EmoticonMaxCount
            
            if loc + len > emoticonsArr.count {
                len = emoticonsArr.count - loc
            }
            
            let range: NSRange = NSRange(location: loc, length: len)
            //截取数组
            let emo = (emoticonsArr as NSArray).subarrayWithRange(range) as! [CZEmoticons]
            
            temArray.append(emo)
        }
        return temArray
    }
    // MARK: - 获取图片表情描述
    func getSearchChs(chs: String) -> CZEmoticons? {
        for e in defaultEmoticonsArray {
            if e.chs == chs {
                return e
            }
        }
        
        for e in lxhArray {
            if e.chs == chs {
                return e
            }
        }
        
        return nil
    }
}






